package demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class GetFileSizeToFile {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\workspacePractice\\practice\\src\\main\\java\\yuanwenjian");
        File[] files = file.listFiles();
        String pathBig = "C:\\Users\\dzf\\Desktop\\fenlei\\Big\\";
        String pathSmall = "C:\\Users\\dzf\\Desktop\\fenlei\\Small\\";
        for (File file1 : files) {
            if (3000 < file1.length()) {
                File file2 = new File(pathBig);
                if (!file2.exists()){ //判断目标文件目录是否存在
                    file2.mkdirs(); //不存在，新建目录
                }
                FileOutputStream outputStream = new FileOutputStream(pathBig+ file1.getName());
                FileToFile(file1, outputStream);
            } else {
                File file2 = new File(pathSmall);
                if (!file2.exists()) {//判断目标文件目录是否存在
                    file2.mkdirs();//不存在，新建目录
                }
                FileOutputStream outputStream = new FileOutputStream(pathSmall+ file1.getName());
                FileToFile(file1, outputStream);
            }
        }
    }

    /**
     *
     * @Description 将文件输出到另一个路径下
     * @author dzf
     * @date 2022/5/25 11:00
     * @params [file1, outputStream]
     * @return void
     * @Version1.0
     **/
    private static void FileToFile(File file1, FileOutputStream outputStream) throws IOException {
        byte[] bytes = new byte[100];
        FileInputStream inputStream = new FileInputStream("D:\\workspacePractice\\practice\\src\\main\\java\\yuanwenjian\\" + file1.getName());
        int hasRead = 0;
        while ((hasRead=inputStream.read(bytes))>0){
            outputStream.write(bytes,0,hasRead);
        }
    }
}
